<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    function myPro(callback) {
        if (callback === undefined) {
            var err = new TypeError("Promise resolver undefined is not a function");
            throw err;
        }
        if (typeof callback !== "function") {
            var err = new TypeError("Promise resolver " + callback + " is not a function");
            throw err;
        }

        var obj = {
            PromiseState: "pending",
            PromiseResult: undefined,
        };

        function resolveHandler(data) {
            if (obj.PromiseState != "pending") return false;
            obj.PromiseState = "fulfilled";
            obj.PromiseResult = data;
        }
        function rejectHandler(data) {
            if (obj.PromiseState != "pending") return false;
            obj.PromiseState = "rejected";
            obj.PromiseResult = data;
        }

        try {
            callback(resolveHandler, rejectHandler);
        } catch (err) {
            // 错误
            // obj.PromiseState = "rejected";
            // obj.PromiseResult = err;

            rejectHandler(err);
        }

        return obj;
    }


    var p = myPro(function (resolve, reject) {
        setTimeout(function () {
            resolve(1);
            console.log(p);
        }, 2000)
    })
    console.log(p);




</script>

</html>